package jee.boot.module.bpm.table.mysql.model;

import jee.boot.common.utils.StringUtils;

/**
 * 带精度的数值
 */
public class DecimalField extends FormField {
    public DecimalField() {
        setLength(16);
        setAccuracy(2);
        setColumnType("DECIMAL");
        setNullable(true);
        setCreate(true);
    }

    /**
     * 新建表字段的sql
     *
     * @return
     */
    @Override
    public String getCreateColumn() {
        StringBuilder sql = new StringBuilder();
        sql.append(getColumnName()).append(" ");
        sql.append(getColumnType()).append("(").append(getLength()).append(",").append(getAccuracy()).append(")").append(" ");
        if (!getNullable()) {
            sql.append("NOT NULL").append(" ");
        } else {
            sql.append("DEFAULT").append(" ");
            if (getDefaultValue() != null) {
                sql.append(getDefaultValue()).append(" ");
            } else {
                sql.append("NULL").append(" ");
            }
        }
        sql.append("COMMENT").append(" ").append("'").append(getComment()).append("'");
        return sql.toString();
    }

    /**
     * 复写equals,用于确认字段的变更
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof DecimalField)) {
            return false;
        }
        DecimalField eobj=(DecimalField) obj;
        if(!StringUtils.equals(this.getComment(),eobj.getComment())){
            return false;
        }
        if(!this.getNullable().equals(eobj.getNullable())){
            return false;
        }
        if(!StringUtils.equals(this.getDefaultValue()+"",eobj.getDefaultValue()+"")){
            return false;
        }
        if(!this.getLength().equals(eobj.getLength())){
            return false;
        }
        if(!this.getAccuracy().equals(eobj.getAccuracy())){
            return false;
        }
        return true;
    }
}
